from typing import Generic, TypeVar, Optional, Any, Dict, Union, List
from pydantic import BaseModel, Field

T = TypeVar('T')

class ResponseModel(BaseModel, Generic[T]):
    """统一API响应模型"""
    code: int = Field(200, description="状态码，200表示成功，其他表示失败")
    msg: str = Field("success", description="响应消息")
    data: Optional[T] = Field(None, description="响应数据")

# 常用响应状态码
class ResponseCode:
    SUCCESS = 200
    BAD_REQUEST = 400
    UNAUTHORIZED = 401
    FORBIDDEN = 403
    NOT_FOUND = 404
    INTERNAL_ERROR = 500

# 响应工厂函数
def success_response(data: Any = None, msg: str = "success") -> Dict[str, Any]:
    """成功响应"""
    return {"code": ResponseCode.SUCCESS, "data": data, "msg": msg}

def error_response(code: int = ResponseCode.BAD_REQUEST, msg: str = "error") -> Dict[str, Any]:
    """错误响应"""
    return {"code": code, "data": None, "msg": msg}

def not_found_response(msg: str = "资源不存在") -> Dict[str, Any]:
    """404响应"""
    return {"code": ResponseCode.NOT_FOUND, "data": None, "msg": msg}

def unauthorized_response(msg: str = "未授权") -> Dict[str, Any]:
    """401响应"""
    return {"code": ResponseCode.UNAUTHORIZED, "data": None, "msg": msg}

def forbidden_response(msg: str = "禁止访问") -> Dict[str, Any]:
    """403响应"""
    return {"code": ResponseCode.FORBIDDEN, "data": None, "msg": msg}

def server_error_response(msg: str = "服务器内部错误") -> Dict[str, Any]:
    """500响应"""
    return {"code": ResponseCode.INTERNAL_ERROR, "data": None, "msg": msg} 